﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WS.EKA.ServiceInterface;
using WS.EKA.DataAccess;
using WS.EKA.Model;

namespace WS.EKA.ServiceImpl
{
    public class RegionService:IRegion
    {
        public List<Model.Region> GetProvinceList()
        {
            var list = RegionContext.GetByConditions(t => t.CategoryLevel == 1 && t.IsDeleted == 0, x => x.OrderBy(t => t.OrderID));
            return list;
        }

        /// <summary>
        /// Get Region by OrderID
        /// </summary>
        /// <param name="parentId">OrderId</param>
        /// <returns></returns>
        public List<Model.Region> GetByParentId(int parentId)
        {
            var list = RegionContext.GetByConditions(
                t => t.IsDeleted == 0 && t.FatherID==parentId
                , x => x.OrderBy(t => t.OrderID));
            return list;
        }
        public string GetParentCode(string code)
        {
            List<string> list = new List<string>();
            list.Add(code);

            Region region = RegionContext.Get(t => t.Code == code);
            if (region == null)
                return "";

            for (int i = region.CategoryLevel; i > 1; i--)
            {
                if (region == null)
                    break;
                region = GetFather(region.FatherID);
                list.Add(region.Code);
            }
       
            list.Reverse();
            return string.Join(",", list.ToArray());
        }

        private Region GetFather(int fatherId)
        {
            return RegionContext.Get(t => t.OrderID == fatherId);
        }
    }
}
